package br.com.abelha.manager.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import br.com.abelha.manager.entity.PlanoSaude;

/**
 * @author dalsonsouza
 */

public class DAOPlanoSaude {
	
	private DBAdapter dbAdapter;
	
	public DAOPlanoSaude(Context context) {
		dbAdapter = new DBAdapter(context).open();
	}
	
	public Cursor listAllCursor() {
		String[] columns = new String[]{PlanoSaude.ID, PlanoSaude.DESCRICAO};
		PlanoSaude planoSaude = new PlanoSaude();
		Cursor cursor = dbAdapter.query(planoSaude.getTableName(), 
						columns , 
						null, 
						null, 
						null, 
						null);
		
		return cursor;
	}
	
	public List<PlanoSaude> listAll() {
		PlanoSaude planoSaude = new PlanoSaude();
		List<PlanoSaude> list = new ArrayList<PlanoSaude>();
		Cursor cursor = listAllCursor();
		
		if(cursor == null) {
			planoSaude = null;
		} else {
			cursor.moveToFirst();
			while(!cursor.isAfterLast()) {
				Integer id = cursor.getInt(cursor.getColumnIndex(PlanoSaude.ID));
				String descricao = cursor.getString(cursor.getColumnIndex(PlanoSaude.DESCRICAO));
				
				planoSaude.setId(id);
				planoSaude.setDescricao(descricao);
				list.add(planoSaude);
				
				planoSaude = new PlanoSaude();
				cursor.moveToNext();
			}
			
			if(!cursor.isClosed()) cursor.close();
		}
		
		return list;
	}
}
